package com.xiaotu.spider.project.pageprocessor;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;

/**
 * @类名 TmailProTypePageProcessor
 * @日期 2017年5月9日
 * @作者 高海军
 * @功能 天猫品类爬取
 */
@Component("TmallProTypePageProcessor")
public class TmallProTypePageProcessor extends AUrlPageProcessor
{
	private static final String[] TITLES = new String[] { "title1", "title2",
			"title3" };
	
	private JSONArray array = new JSONArray();
	
	private static final int SIZE = 16;
	
	@Override
	public void process(Page page)
	{
		if (page.getUrl().toString()
				.contains("https://aldh5.tmall.com/recommend2.htm"))
			this.thrProcess(page);
		else
			this.oneProcess(page);
	}
	
	private void thrProcess(Page page)
	{
		String json = page.getRawText().substring(
				page.getRawText().indexOf("(") + 1,
				page.getRawText().length() - ")".length());
		JSONObject jsonObj = JSONObject.fromObject(json);
		
		JSONArray twoArr = ((JSONObject) page.getRequest().getExtra("oneLel"))
				.getJSONArray("children");
		for (int i = 0; i < twoArr.size(); i++)
		{
			String key = twoArr.getJSONObject(i).getString("appId");
			twoArr.getJSONObject(i).put("children", new JSONArray());
			JSONArray thrArr = jsonObj.getJSONObject(key).getJSONArray("data");
			for (int j = 0; j < thrArr.size(); j++)
			{
				if (!"true".equals(thrArr.getJSONObject(j).getString("isUse")))
					continue;
				JSONObject thrObj = new JSONObject();
				thrObj.put("title", thrArr.getJSONObject(j).getString("title"));
				twoArr.getJSONObject(i).getJSONArray("children").add(thrObj);
			}
		}
		
		array.add(
				((JSONObject) page.getRequest().getExtra("oneLel")).toString());
	}
	
	private void saveJson()
	{
		new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				while (true)
				{
					if (array.size() == SIZE)
					{
						FileUtil.writeFile("D://tmall.txt", array.toString());
						break;
					}
					try
					{
						Thread.sleep(1000);
					}
					catch (InterruptedException e)
					{
						e.printStackTrace();
					}
				}
				
			}
		}).start();
	}
	
	private void oneProcess(Page page)
	{
		JSONObject jsonObj = JSONObject.fromObject(page.getHtml()
				.xpath("//div[@id='J_defaultData']/text()").get());
		JSONArray oneLelArr = jsonObj.getJSONObject("page").getJSONObject("100")
				.getJSONArray("categoryMainLines");
		
		for (int i = 0; i < oneLelArr.size(); i++)
		{
			JSONObject oneObj = new JSONObject();
			oneObj.put("title", this.getTitle(oneLelArr.getJSONObject(i)));
			oneObj.put("children", new JSONArray());
			
			JSONArray twoArr = jsonObj.getJSONObject("page")
					.getJSONObject("100").getJSONArray("hotWordType" + (i + 1));
			StringBuffer buffer = new StringBuffer();
			for (int j = 0; j < twoArr.size(); j++)
			{
				if (!"true".equals(twoArr.getJSONObject(j).getString("isUse")))
					continue;
				JSONObject twoObj = new JSONObject();
				twoObj.put("title", twoArr.getJSONObject(j).getString("title"));
				twoObj.put("appId", twoArr.getJSONObject(j).getString("appId"));
				buffer.append(
						SepratorUtil.SEP_COMMA_EN + twoObj.getString("appId"));
				oneObj.getJSONArray("children").add(twoObj);
			}
			
			page.getRequest().getExtras().put("oneLel", oneObj);
			PageUtils.addTargetRequest(page,
					"https://aldh5.tmall.com/recommend2.htm?notNeedBackup=true&appId="
							+ buffer.substring(
									SepratorUtil.SEP_COMMA_EN.length())
							+ "&callback=jsonp_6080193",
					page.getRequest().getExtras());
		}
		
	}
	
	private String getTitle(JSONObject oneObj)
	{
		StringBuffer buffer = new StringBuffer();
		for (String title : TITLES)
		{
			if (oneObj.has(title)
					&& StringUtils.isNotEmpty(oneObj.getString(title)))
				buffer.append(
						SepratorUtil.SEP_SLASH_EN + oneObj.getString(title));
		}
		return buffer.substring(SepratorUtil.SEP_SLASH_EN.length());
	}
	
	@Override
	protected String getSpiderType()
	{
		return "Tmall";
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return Constants.PageDataType.SEARCH_LIST;
	}
	
	@Override
	protected String[] getStartRequestUrls()
	{
		saveJson();
		return new String[] { "https://www.tmall.com/" };
	}
	
}
